<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class student extends  BaseApiController
{
   public function index(){

   }
   
   public function list_by_page(){
	 $token=$this->verifyToken();
	 if($token){
		 $keys=[
			 ["name","option","学员姓名",""],
			 ["mobile","option","电话号码",""],
			 ["address","option","机构地址",""],
			 ["school","option","机构地址",""],
			 ["grade","option","机构地址",""],
			 ["source","option","机构地址",""],
			 ["recommander","option","机构地址",""],
			 ["address","option","家庭住址",""],
			 ["idcard","option","身份证号码",""],
			 ["memo","option","备注",""],
		 ];
		 
		 $data=$this->getFields($keys);

		 foreach($data as $key=>$value){
			 $this->db->like($key,$value); 
		 }
		 $this->db->where("schcode",$token["schcode"]);
		 $res=$this->db->select("count(*) as count")->get("student")->row_array();
		 $numrows=(int)$res["count"];
		 
		 foreach($data as $key=>$value){
			 $this->db->like($key,$value); 
		 }
		 $this->db->where("schcode",$token["schcode"]);
		 $rows=$this->db->get("student")->result_array();
		 
		 $this->WRITE("code",1);
		 $this->WRITE("numrows",$numrows);
		 $this->WRITE("list",$rows);
		 $this->RESPONSE();	 
	}
   }
   
   public function create(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				 ["name","required","学员姓名",""],
				 ["mobile","required","电话号码",""],
				 ["mobiletag","option","电话号码关系",""],
				 ["birth","option","出生日期",""],
				 ["school","option","就读学校",""],
				 ["grade","option","就读年级",""],
				 ["source","option","学员来源",""],
				 ["recommander","option","推荐人",""],
				 ["mobile2","option","备用号码",""],
				 ["mobile2tag","option","备用号码关系",""],
				 ["idcard","option","身份证号码",""],
				 ["address","option","家庭地址",""],
				 ["memo","option","备注",""],
		   ];
		   $data=$this->getFields($keys);
		   $data["stuid"]=uniqid();
		   $data["schcode"]=$token["schcode"];
		   $res=$this->db->insert("student",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","新增学员成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","新增学员失败!");
			   $this->RESPONSE();	
		   }
	   }

   }
   
   public function update(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				["stuid","required","学员标识",""],
				["name","required","学员姓名",""],
				["mobile","required","电话号码",""],
				["mobiletag","option","电话号码关系",""],
				["gender","required","性别","男"],
				["birth","option","出生日期",""],
				["school","option","就读学校",""],
				["grade","option","就读年级",""],
				["source","option","学员来源",""],
				["recommander","option","推荐人",""],
				["mobile2","option","备用号码",""],
				["mobile2tag","option","备用号码关系",""],
				["idcard","option","身份证号码",""],
				["address","option","家庭地址",""],
				["memo","option","备注",""],
		   ];
		   
		   $data=$this->getFields($keys);
		   
		   $res=$this->db->where("stuid",$data["stuid"])->update("student",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","更新学员信息成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","更新学员信息失败!");
			   $this->RESPONSE();	
		   }
   	   }
   
   }
   
   
   public function delete(){
	   $token=$this->verifyToken();
	   if($token){
		   $data=$this->getFields(array(["stuid","required","学员标识",""]));
		   
		   $res=$this->db->where("stuid",$data["stuid"])->delete("student",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","删除学员信息成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","删除学员信息失败!");
			   $this->RESPONSE();	
		   }
		}
   }
   
   public function detail(){
	   $token=$this->verifyToken();
	   if($token){
	   		   $data=$this->getFields(array(["stuid","required","学员标识",""]));
			   
			   $row=$this->db->where("stuid",$data["stuid"])->get("student")->row_array();
			   
			   $this->WRITE("code",1);
			   $this->WRITE("info",$row);
			   $this->WRITE("msg","操作成功!");
			   $this->RESPONSE();	
	   }
	  
	   
   }
}